package com.example.assign2_list;

import com.example.assign2_list.Mp3Service.Mp3Binder;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class Mp3Activity extends Activity {
	private Mp3Service mBoundService;
	private NotificationManager notifManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_mp3);
        
        /*	Start Mp3Service */
    	Intent intent = new Intent(this, Mp3Service.class);
		bindService(intent, mConnection, BIND_AUTO_CREATE);
    }
    
    public void prevTrack(View v){
    	mBoundService.prev();
    }
    
    public void nextTrack(View v){
    	mBoundService.next();
    }
    
    public void playTrack(View v){
    	mBoundService.play();
    	
		/* 1. Setup Notification Builder */			
		Notification.Builder builder = new Notification.Builder(this); 
		
		/* 2. Configure Notification Alarm */
		builder.setSmallIcon(android.R.drawable.ic_media_play)
			.setWhen(System.currentTimeMillis())
			.setAutoCancel(true);
			
		/* 3. Configure Drop-down Action */
		builder.setContentTitle("Mp3 Player")
				.setContentText("Click to resume player");
		Intent intent = new Intent(this, Mp3Activity.class);   // Notification intent
		PendingIntent notifIntent = PendingIntent.getActivity(this, 0, intent, 0);
		builder.setContentIntent(notifIntent);
		
		/* 4. Create Notification and use Manager to launch it */
		Notification notification = builder.build();	
		String ns = Context.NOTIFICATION_SERVICE;
		notifManager = (NotificationManager) getSystemService(ns);
		notifManager.notify(1, notification);
    }
    
    public void pauseTrack(View v){
    	mBoundService.pause();
    }
    
    /*	Create connection to be able to get information from service */
    private ServiceConnection mConnection = new ServiceConnection(){
		public void onServiceConnected(ComponentName name, IBinder service) {
			Mp3Binder mBinder = (Mp3Binder)service;	//Binder in service
			mBoundService = mBinder.getService();
		}
		
		/*	on crash or if anything other fail with the service	*/
		public void onServiceDisconnected(ComponentName name) {
			mBoundService = null;
		}
    };
}
